package org.zebra.message;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertiesPropertySource;

import java.util.Map;
import java.util.Properties;

/**
 * @author zhanghongbin
 */
public class MessagePostProcessor implements EnvironmentPostProcessor {

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        String supplier = environment.getProperty("zebra.message.sms.supplier", "");
        String accessKeyId = environment.getProperty("zebra.message.sms.access-key-id", "");
        String accessKeySecret = environment.getProperty("zebra.message.sms.access-key-secret", "");
        String signature = environment.getProperty("zebra.message.sms.signature", "");
        // String templateId = environment.getProperty("zebra.message.sms.template-id", "");
        String sdkAppId = environment.getProperty("zebra.message.sms.sdk-app-id", "");
        Properties properties = new Properties();
        properties.put("sms.isPrint", false);
        PropertiesPropertySource propertySource = new PropertiesPropertySource("messageProperties", properties);
        Map<String, Object> sourceMap = propertySource.getSource();
        // sms配置
        if (!supplier.isEmpty() && !accessKeyId.isEmpty() && !accessKeySecret.isEmpty() && !signature.isEmpty()) {
            sourceMap.put("sms.config-type", "yaml");
            sourceMap.put("sms.blends.sms-config.supplier", supplier);
            sourceMap.put("sms.blends.sms-config.access-key-id", accessKeyId);
            sourceMap.put("sms.blends.sms-config.access-key-secret", accessKeySecret);
            sourceMap.put("sms.blends.sms-config.signature", signature);
            // sourceMap.put("sms.blends.sms-config.template-id", templateId);
            if (!sdkAppId.isEmpty()) {
                sourceMap.put("sms.blends.sms-config.sdk-app-id", sdkAppId);
            }
        }
        // oa配置
        String oaSupplier = environment.getProperty("zebra.message.oa.supplier", "");
        String oaTokenId = environment.getProperty("zebra.message.oa.tokenId", "");
        String oaSign = environment.getProperty("zebra.message.oa.sign", "");
        sourceMap.put("sms-oa.config-type", "yaml");
        if (!oaSupplier.isEmpty() && !oaTokenId.isEmpty()) {
            sourceMap.put("sms-oa.oas.oa-config.isEnable", true);
            sourceMap.put("sms-oa.oas.oa-config.supplier", oaSupplier);
            sourceMap.put("sms-oa.oas.oa-config.tokenId", oaTokenId);
        }
        if (!oaSign.isEmpty()) {
            sourceMap.put("sms-oa.oas.oa-config.sign", oaSign);
        }
        sourceMap.put("sms-oa.core-pool-size", 20); // 核型线程数量
        sourceMap.put("sms-oa.queue-capacity", 20); // 队列容量
        sourceMap.put("sms-oa.max-pool-size", 20); // 最大线程池数量
        environment.getPropertySources().addLast(propertySource);
    }
}
